package com.yunhe.sms.service.impl;


import com.yunhe.sms.dao.system.UserDao;
import com.yunhe.sms.dao.system.impl.UserDaoImpl;
import com.yunhe.sms.dto.Menu;
import com.yunhe.sms.entity.system.Permission;
import com.yunhe.sms.service.LoginService;
import com.yunhe.sms.service.system.PermissionService;
import com.yunhe.sms.service.system.impl.PermissionServiceImpl;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/16 0016.
 */
public class LoginServiceImpl implements LoginService{

    private UserDao userDao = new UserDaoImpl();
    private PermissionService permissionService = new PermissionServiceImpl();

    @Override
    public List<Menu> leftMenu(Long roleId) {
        List<Menu> menuList = new ArrayList<>();
        Map<Long,Menu> menuMap = new HashMap<>();
        List<Permission> modulePermi = permissionService.findPermiMenuByRole(roleId, 1);
        List<Permission> pagePermi = permissionService.findPermiMenuByRole(roleId, 2);
        for(Permission p:modulePermi){
            Menu menu = new Menu(p.getId(),p.getName(),p.getUri());
            menuList.add(menu);
            menuMap.put(p.getId(),menu);
        }

        for(Permission p:pagePermi){
            Menu menu = new Menu(p.getId(),p.getName(),p.getUri());
            Menu parentMenu = menuMap.get(p.getParentId());
            if(null != parentMenu){
                parentMenu.getSubmenus().add(menu);
            }
        }
        return menuList;
    }


    @Override
    public List<Menu> findPermiMenu(Long roleId) {
          List<Permission> permiList = permissionService.findByRole(roleId);
                    List<Menu> menuList = new ArrayList<>();
                    for(Permission p : permiList){
                       if(p.getType()==1){
                           Menu parentMenu = new Menu(p.getId(),p.getName(),p.getUri());
                           for(Permission c : permiList){
                               if(c.getType() == 2){
                                   if(null != c.getParentId() && c.getParentId().equals(p.getId())){
                                       Menu pageMenu = new Menu(c.getId(),c.getName(),c.getUri());
                                       parentMenu.getSubmenus().add(pageMenu);
                                   }
                               }
                           }
                           menuList.add(parentMenu);
                       }
                    }
             return   menuList;
    }



    @Override
    public Map<String, Permission> allPermission(Long roleId) {
        Map<String,Permission> permissionMap = new HashMap<>();
        List<Permission> allPermission = permissionService.findByRole(roleId);
        for(Permission p : allPermission){
            permissionMap.put(p.getUri(),p);
        }
        return permissionMap;
    }

    @Override
    public void autoLogin(HttpServletRequest req, HttpServletResponse resp, String autoLogin, String userName, String password){
        if(null != autoLogin && autoLogin.equals("自动登录")){
            Cookie cookie = new Cookie("userName",userName);
            cookie.setMaxAge(3600*24*7);
            cookie.setPath("/");
            resp.addCookie(cookie);

            Cookie cookie2 = new Cookie("password",password);
            cookie2.setMaxAge(3600*24*7);
            cookie2.setPath("/");
            resp.addCookie(cookie2);

        }else{
            Cookie[] cookies = req.getCookies();
            if(null != cookies){
                for(Cookie c : cookies){
                    if(c.getName().equals("userName")){
                        c.setMaxAge(0);
                        break;
                    }
                }
            }
        }
    }



    /*@Override
    public Boolean login(HttpServletRequest req,HttpServletResponse resp,String userName, String password, String autoLogin) {

        User user = userDao.findOne(userName);
        Boolean flag = false;
        if(null != user && user.getPassword().equals(password)){
            flag = true;

            HttpSession session = req.getSession();
            session.setAttribute("user",user);

            if(null != autoLogin && autoLogin.equals("自动登录")){
                Cookie cookie = new Cookie("userName",userName);
                cookie.setMaxAge(3600*24*7);
                resp.addCookie(cookie);
            }else{
                Cookie[] cookies = req.getCookies();
                if(null != cookies){
                    for(Cookie c : cookies){
                        System.out.println(c.getName());
                        if(c.getName().equals("userName")){
                           c.setMaxAge(0);
                            break;
                        }
                    }
                }
            }

            List<Permission> rolePermi = permissionService.findByRole(user.getRoleId());

        }
        return flag;
    }*/


}
